<?php

class members_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('members.members_list_model');
		$col = $CI->uri->segment($CI->members_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->members_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->members_list_model->cache->add($k, $order[$CI->members_list_model->cache->load($k)]);
			} else
				$CI->members_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->members_list_model->url_prefix.'/'.$CI->members_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('members.members_list_model');
		
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->members_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->db->delete(MEMBERS, array(MEMBERS_MEMBER_ID=>$id));
				} 
			}
		}
		
		// message
		if ($flag)
			$CI->members_list_model->cache->add('msg', $CI->lang->line('list_delete_all'));
		else
			$CI->members_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->members_list_model->error_string);
		
		// go to original page
		redirect($CI->members_list_model->url_prefix.'/'.$CI->members_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('members.members_list_model');
		$id = $CI->uri->segment($CI->members_list_model->item_segment);
		
		if ($CI->members_list_model->check_delete($id))
		{
			$CI->db->delete(MEMBERS, array(MEMBERS_MEMBER_ID=>$id));			
			// message
			$CI->members_list_model->cache->add('msg', $CI->lang->line('list_delete'));
		} else
			// message
			$CI->members_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->members_list_model->error_string);
		
		// go to original page
		redirect($CI->members_list_model->url_prefix.'/'.$CI->members_list_model->start);
	}
	
	function saveItem()
	{
		global $CI;
		// model
		$CI->load->module_model('members.members_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        if ($this->_validate($CI->customers_detail_model->item) == FALSE)
		{
			// message
			$CI->members_detail_model->cache->add('msg', $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->members_detail_model->cache->add('post', $_POST);
			
			// go to original page
			redirect($CI->members_detail_model->url_prefix.'/'.$CI->members_detail_model->start.'/'.$CI->members_detail_model->item);
		}	
				
		// save to members
		if ($CI->members_detail_model->hasMEMBERS())
		{
			// update
            $data = array(
				MEMBERS_LOGIN_EMAIL=>$CI->input->post('login_email'),
				MEMBERS_FULL_NAME=>$CI->input->post('fullname'),
				MEMBERS_ADDRESS=>$CI->input->post('address'),
				MEMBERS_PHONE=>$CI->input->post('phone'),
				MEMBERS_INFO=>$CI->input->post('infomation'),
				MEMBERS_TYPE=>$CI->input->post('type'),
				MEMBERS_STATUS=>(isset($_POST['active'])?1:0),
				//MEMBERS_IMAGE  => $CI->input->post('imagepath'),
				MEMBERS_UPDATE_DATE => now_to_mysql()
			);
			if ($CI->input->post('password')!=''&&$CI->input->post('repassword')!=''&&$CI->input->post('password')==$CI->input->post('password')){
				$data[CUSTOMERS_PW] =  md5($CI->input->post('password'));
			}
			else{
				echo 'Please enter Password.';
			}
			if ($_FILES['imagepath']['tmp_name']){
				$data[MEMBERS_IMAGE] =  $_FILES["imagepath"]["name"];			
			}
            $CI->db->where(MEMBERS_MEMBER_ID, $CI->members_detail_model->item);
			$CI->db->update(MEMBERS, $data);
		} else {
			// insert
			$data = array(
				MEMBERS_MEMBER_ID=>$CI->members_detail_model->item,
				MEMBERS_FULL_NAME=>$CI->input->post('fullname'),
				MEMBERS_LOGIN_EMAIL=>$CI->input->post('login_email'),
				MEMBERS_ADDRESS=>$CI->input->post('address'),
				MEMBERS_PHONE=>$CI->input->post('phone'),
				MEMBERS_INFO=>$CI->input->post('infomation'),
				MEMBERS_TYPE=>$CI->input->post('type'),
				MEMBERS_STATUS=>(isset($_POST['active'])?1:0),
				MEMBERS_UPDATE_DATE => now_to_mysql(),
				MEMBERS_DATE => now_to_mysql()
			);
			if ($CI->input->post('password')!=''&&$CI->input->post('repassword')!=''&&$CI->input->post('password')==$CI->input->post('password')){
				$data[CUSTOMERS_PW] =  md5($CI->input->post('password'));			
			}			
			if ($_FILES['imagepath']['tmp_name']){
				$data[MEMBERS_IMAGE] =  $_FILES["imagepath"]["name"];			
			}
			$CI->db->insert(MEMBERS, $data);
		}		
		//upload
		$this->upload();
		
		// message
		$CI->members_detail_model->cache->add('msg', $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->members_detail_model->url_prefix2.'/'.$CI->members_detail_model->start);
	}		
	function upload(){
		//upload	
		global $CI;	
		$CI->load->library("fckeditor");
		$path = $CI->fckeditor->UploadPath.'/images';;			
        $path = rtrim($_SERVER['DOCUMENT_ROOT'], "/").$path;
		if ($_FILES['imagepath']['tmp_name'])
		{
			
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'gif|jpg|jpeg|png';
			$config['max_size'] = '2048';
			$config['max_width'] = '1024';
			$config['max_height'] = '768';			
			$config['max_filename'] = '54';
			$CI->load->library('upload', $config);
			if (!$CI->upload->do_upload('imagepath'))
			{
				//$CI->upload->display_errors();
				set_flash_message($CI->upload->display_errors());
			} 
			else{
				$file_name = $CI->upload->file_name;
				$new_width= 132;
				$new_height= 113;
				$uppath = $path."/".$file_name;
				list($width, $height, $type, $attr) = @getimagesize($uppath);
				if($width>0&&$height>0){
					//$new_height = (int)($new_width*$height/$width);
					$new_width = (int)($new_height*$width/$height);
				}

				// create thumbnail
				$CI->load->module_helper('image.create_thumbnail');
				create_thumbnail($path.'/'.$file_name, $path.'/thumb/'.$file_name, $new_width, $new_height);
				/*
				//Update image in DB				
				if($file_name){
					$data = array (	MEMBERS_IMAGE=>$file_name 	);
					$CI->db->where(MEMBERS_PRODUCT_ID, $CI->members_detail_model->item);
					$CI->db->update($this->_table_name, $data);
				}
				*/
			}
		}
	}
	//validate function
	function _validate($edit_id){
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('fullname', 'fullname', 'trim|required');
		$CI->form_validation->set_rules('login_email', 'login_email', 'trim|required|valid_emails');
		if($edit_id){
			if(isset($_POST['repassword'])&&($_POST['repassword'])){
				$CI->form_validation->set_rules('password', 'Password', 'trim|required|callback__validate_pw');
				$CI->form_validation->_callback_object = $this;
			}
		}
		else{
			$CI->form_validation->set_rules('repassword', 'Re-password', 'trim|required');
			$CI->form_validation->set_rules('password', 'Password', 'trim|required|callback__validate_pw');
			$CI->form_validation->_callback_object = $this;
		}
        return $CI->form_validation->run();
    }
	
	function _validate_pw(){
        $CI = & get_instance();
		$CI->load->library('form_validation');
        $CI->load->helper('form');
        if($CI->input->post('repassword')!=$CI->input->post('password')){
            $CI->form_validation->set_message('_validate_pw', lang('_invalid_pw'));
            return FALSE;
        }
        return TRUE;
    }
	//Change status of member
	function change_status(){
		global $CI;
		//Check language
		$CI->load->module_model('members.members_detail_model');		
		$member_id = $CI->uri->segment(3);	
		$CI->members_detail_model->item = $member_id;
		$member = $CI->members_detail_model->gethasMEMBERS();
		 if($member->{MEMBERS_STATUS} == 1){			
			$data = array(MEMBERS_STATUS => 0);
			$CI->db->where(MEMBERS_MEMBER_ID, $CI->members_detail_model->item);
			$CI->db->update(MEMBERS, $data);
			echo 0;
		} else {
			$data = array(MEMBERS_STATUS => 1);
			$CI->db->where(MEMBERS_MEMBER_ID, $CI->members_detail_model->item);
			$CI->db->update(MEMBERS, $data);
			echo 1;
		}
		die();
	}
	
}

?>